package com.server.service.impl;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import com.server.date.DateType;
import com.server.date.DateUtil;
import com.server.password.MyMD5;
import com.server.service.ICommonDataService;
import com.server.service.IRewardConfigService;
import com.server.service.IUserConfigService;
import com.server.service.IUserManagementService;
import com.server.web.DataVector;
import com.server.web.RequestInput;

@Service("UserManagementService")
public class UserManagementService implements IUserManagementService {
	
	private static final String TABLE_NAME = "reward.user";
	private static final String TABLE_NAME_EMPLOY = "reward.employ";
	private static final String VIEW_NAME = "reward.v_user_employ";
	
	private static Map<String, Integer> months = new HashMap<String, Integer>();

	@Autowired
	private JdbcTemplate jdbcTemplate;
	@Autowired
	private ICommonDataService commonDataService;
	@Autowired
	private IRewardConfigService rewardConfig;
	@Autowired
	private IUserConfigService userConfigService;
	
	@Override
	public void loadMonth(){
		this.months.put("一月", 1);
		this.months.put("二月", 2);
		this.months.put("三月", 3);
		this.months.put("四月", 4);
		this.months.put("五月", 5);
		this.months.put("六月", 6);
		this.months.put("七月", 7);
		this.months.put("八月", 8);
		this.months.put("九月", 9);
		this.months.put("十月", 10);
		this.months.put("十一月", 11);
		this.months.put("十二月", 12);
	}
	
	 /**
	 * 员工添加
	 * @param operator
	 * @param inputMap
	 * @return
	 * @author ServerZhang
	 * @date 2017年3月3日
	 */
	@Override
	public Boolean add(String operator, Map<String, Object> inputMap){
		Object cardObj = inputMap.get("CardID");
		if (cardObj == null) {
			return false;
		}
		String cardId = cardObj.toString();
		//查询是否可添加
		Boolean checkUser = this.checkUser(cardId);
		return checkUser && commonDataService.add(operator, TABLE_NAME, inputMap);
	}
	@Override
	public Boolean addBatch(String operator, List<Map<String, Object>> inputList){
		Map<String, List<Map<String, Object>>> checkUser = this.checkUser(inputList);
		return commonDataService.addBatch(operator, TABLE_NAME, checkUser.get("add"));
	}
	
	 /**
	 * 员工入职
	 * @param operator
	 * @param inputMap
	 * @return
	 * @author ServerZhang
	 * @date 2017年3月3日
	 */
	@Override
	public Boolean employ(String operator, Map<String, Object> inputMap){
		Object cardObj = inputMap.get("CardID");
		if (cardObj == null) {
			return false;
		}
		String cardId = cardObj.toString();
		//查询是否可入职
		Boolean checkEmploy = this.checkEmploy(cardId);
		return checkEmploy && commonDataService.add(operator, TABLE_NAME_EMPLOY, inputMap);
	}
	@Override
	public Boolean employBatch(String operator, List<Map<String, Object>> inputList){
		List<Map<String, Object>> checkEmploy = this.checkEmploy(inputList);
		return commonDataService.addBatch(operator, TABLE_NAME_EMPLOY, checkEmploy);
	}
	 /**
	 * 查询员工是否可入职
	 * @param cardId
	 * @return
	 * @author ServerZhang
	 * @date 2017年3月3日
	 */
	@Override
	public Boolean checkEmploy(String cardId){
		if (StringUtils.isEmpty(cardId)) {
			return false;
		}
		String sql = "select count(CardID) from %s where CardID='%s' and State=1";
		return jdbcTemplate.queryForObject(String.format(sql, TABLE_NAME_EMPLOY,cardId), Integer.class) < 1;
	}
	 /**
	 * 查询并返回可入职员工列表
	 * @param cardId
	 * @return
	 * @author ServerZhang
	 * @date 2017年3月3日
	 */
	@Override
	public List<Map<String, Object>> checkEmploy(List<Map<String, Object>> inputList){
		List<Map<String, Object>> returnList = new ArrayList<Map<String,Object>>();
		List<Map<String, Object>> tempList = new ArrayList<Map<String,Object>>();
		Iterator<Map<String, Object>> iterator = inputList.iterator();
		StringBuffer cardBuffer = new StringBuffer();
		while(iterator.hasNext()){
			Map<String, Object> next = iterator.next();
			//去除不必要字段
			next.remove("Id");
			next.remove("State");
			next.remove("LeaveTime");
			next.remove("LeaveNote");
			next.remove("Mender");
			next.remove("Creater");
			next.remove("CreaterName");
			next.remove("CreateTime");
			next.remove("MenderName");
			next.remove("UpdateTime");
			next.remove("Note");
			next.remove("Valid");
			Object cardObj = next.get("CardID");
			if (cardObj != null) {
				tempList.add(next);//无身份证的不予入职
				if (cardBuffer.length() > 0) {
					cardBuffer.append(",");
				}
				cardBuffer.append("'").append(cardObj.toString()).append("'");
			}
		}
		String sql = "select * from %s where CardID in(%s) and state=1";
		List<Map<String, Object>> employList = jdbcTemplate.queryForList(String.format(sql, TABLE_NAME_EMPLOY,cardBuffer.toString()));
		StringBuffer employed = new StringBuffer();
		Iterator<Map<String, Object>> employIterator = employList.iterator();
		while(employIterator.hasNext()){//在职员工不能继续入职
			Map<String, Object> row = employIterator.next();
			Object cardObj = row.get("CardID");
			if (cardObj != null) {
				if (employed.length() > 0) {
					employed.append(",");
				}
				employed.append(cardObj.toString());
			}
		}
		String cardIds = employed.toString();
		returnList.addAll(tempList);
		Iterator<Map<String, Object>> returnIterator = tempList.iterator();
		while(returnIterator.hasNext()){
			Map<String, Object> map = returnIterator.next();
			String cardId = map.get("CardID").toString();
			if (cardIds.contains(cardId)) {
				returnList.remove(map);
			}
		}
		return returnList;
	}
	 /**
	 * 员工数据修改
	 * @param operator
	 * @param inputMap
	 * @return
	 * @author ServerZhang
	 * @date 2017年3月3日
	 */
	@Override
	public Boolean update(String operator, Map<String, Object> inputMap){
		Object cardObj = inputMap.get("CardID");
		if (cardObj == null) {
			return false;
		}
		String cardId = cardObj.toString();
		return commonDataService.update(operator, TABLE_NAME, "CardID", cardId, inputMap);
	}
	@Override
	public Boolean updateBatch(String operator, List<Map<String, Object>> inputList){
		return commonDataService.updateBatch(operator, TABLE_NAME, "CardID", inputList);
	}
	/**
	 * 查询员工能否添加
	 * @param cardId
	 * @return
	 * @author ServerZhang
	 * @date 2017年3月3日
	 */
	@Override
	public Boolean checkUser(String cardId){
		if (StringUtils.isEmpty(cardId)) {
			return false;
		}
		String sql = "select count(CardID) from %s where CardID='%s'";
		return jdbcTemplate.queryForObject(String.format(sql, TABLE_NAME,cardId), Integer.class) < 1;
	}
	/**
	 * 查询并返回可添加员工列表和可修改员工列表
	 * @return
	 * @author ServerZhang
	 * @date 2017年3月3日
	 */
	@Override
	public Map<String,List<Map<String, Object>>> checkUser(List<Map<String, Object>> inputList){
		Map<String,List<Map<String, Object>>> returnMap = new HashMap<String,List<Map<String, Object>>>();
		List<Map<String, Object>> tempList = new ArrayList<Map<String,Object>>();
		List<Map<String, Object>> addList = new ArrayList<Map<String,Object>>();
		List<Map<String, Object>> updateList = new ArrayList<Map<String,Object>>();
		Iterator<Map<String, Object>> iterator = inputList.iterator();
		StringBuffer cardBuffer = new StringBuffer();
		while(iterator.hasNext()){
			Map<String, Object> next = iterator.next();
			Object cardObj = next.get("CardID");
			if (cardObj != null) {
				tempList.add(next);//无身份证的不予添加
				if (cardBuffer.length() > 0) {
					cardBuffer.append(",");
				}
				cardBuffer.append("'").append(cardObj.toString()).append("'");
			}
		}
		String sql = "select * from %s where CardID in(%s)";
		List<Map<String, Object>> employList = jdbcTemplate.queryForList(String.format(sql, TABLE_NAME,cardBuffer.toString()));
		StringBuffer usersBuffer = new StringBuffer();
		Iterator<Map<String, Object>> employIterator = employList.iterator();
		while(employIterator.hasNext()){//员工表数据不得重复
			Map<String, Object> row = employIterator.next();
			Object cardObj = row.get("CardID");
			if (cardObj != null) {
				if (usersBuffer.length() > 0) {
					usersBuffer.append(",");
				}
				usersBuffer.append(cardObj.toString());
			}
		}
		String cardIds = usersBuffer.toString();
		Iterator<Map<String, Object>> returnIterator = tempList.iterator();
		while(returnIterator.hasNext()){
			Map<String, Object> map = returnIterator.next();
			String cardId = map.get("CardID").toString();
			if (cardIds.contains(cardId)) {
				updateList.add(map);
			}else {
				addList.add(map);
			}
		}
		returnMap.put("add", addList);
		returnMap.put("update", updateList);
		return returnMap;
	}
	
	@Override
	public Map<String, Object> deleteByCards(String cardIds){
		Boolean isSuccess = false;
		Integer errorCount = 0;
		Map<String, Object> returnMap = new HashMap<String, Object>();
		if (!StringUtils.isEmpty(cardIds)) {
			int tCount = cardIds.split(",").length;
			StringBuffer sqlBuffer = new StringBuffer();
			sqlBuffer.append("delete from ").append(TABLE_NAME).append(" where CardID in('")
			.append(cardIds.replaceAll(",", "','")).append("')");
			int uCount = jdbcTemplate.update(sqlBuffer.toString());
			if((errorCount = tCount - uCount) == 0){
				isSuccess = true;
			}
		}
		returnMap.put("isSuccess", isSuccess);
		returnMap.put("errorCount", errorCount);
		return returnMap;
	}
	
	@Override
	public DataVector<Map<String, Object>> getList(RequestInput requestInput,
			String orgNo, Integer state,Integer levelKey) {
		String whereStr = null;
		if (orgNo == null || orgNo.equals("all")) {
			whereStr = String.format("State=%s and Manager<=%s", state, levelKey);
		}else{
			whereStr = String.format("OrgNo='%s' and State=%s and Manager<=%s", orgNo, state, levelKey);
		}
		String sortStr = "EmployTime desc";
		return commonDataService.getListData(VIEW_NAME, whereStr, sortStr,
				requestInput);
	}
	@Override
	public List<Map<String, Object>> replaceSourceName(List<Map<String, Object>> list){
		for (int i = 0; i < list.size(); i++) {
			Object sourceName = list.get(i).get("SourceName");
			Object sourceName1 = list.get(i).get("SourceName1");
			if ( sourceName==null ) {
				list.get(i).put("SourceName", sourceName1);
			}
		}
		return list;
	}
	
	@Override
	public List<Map<String, Object>> getList(String orgNo, Integer state, String whereStr) {
		StringBuffer sqlBuffer = new StringBuffer();
		sqlBuffer.append("select * from ").append(VIEW_NAME).append(" where Valid=1");
		if (orgNo != null || "0".equals(orgNo)) {
			sqlBuffer.append(" and OrgNo='").append(orgNo).append("'");
		}
		if (state != null) {
			sqlBuffer.append(" and State=").append(state);
		}
		if (whereStr != null && whereStr.length() > 0) {
			sqlBuffer.append(" and ").append(whereStr);
		}
		
		return jdbcTemplate.queryForList(sqlBuffer.toString());
	}
	
	@Override
	public Map<String, Object> getForm(String employId){
		Map<String, Object> formData = commonDataService.getFormData(VIEW_NAME, "EmployId", employId, null, null);
		return formData;
	}
	/**
	 * 获取某公司在职人员
	 * @param orgNo
	 * @return
	 * @author ServerZhang
	 * @date 2017年3月6日
	 */
	@Override
	public List<Map<String, Object>> getSource(String orgNo){
		String sql = "select u.CardID,u.UserName from reward.user u left outer join reward.employ e on e.CardID=u.CardID and e.State=1 where e.OrgNo='%s'";
		return jdbcTemplate.queryForList(String.format(sql, orgNo));
	}
	/**
	 * 员工添加并入职
	 * @param operator
	 * @param inputMap
	 * @return
	 * @author ServerZhang
	 * @date 2017年3月7日
	 */
	@Override
	public Map<String, Object> addAndEmploy(String operator, 
			Map<String, Object> inputMap, String action){
		//返回信息
		Map<String, Object> inforMap = new HashMap<String, Object>();
		Boolean isSuccess = true;
		String info = "";
		Object cardObj = inputMap.get("CardID");
		Object orgObj = inputMap.get("OrgNo");
		Object natureObj = inputMap.get("Nature");
		Object stObj = inputMap.get("SourceType");
		Object timeObj = inputMap.get("EmployTime");
		Object policyObj = inputMap.get("Policy");
		inputMap.remove("Policy");
		if (cardObj == null) {
			isSuccess = false;
			info = "身份证号有误！";
			inforMap.put("isSuccess", isSuccess);
			inforMap.put("info", info);
			return inforMap;
		}
		if (orgObj == null) {
			isSuccess = false;
			info = "公司信息不正确！";
			inforMap.put("isSuccess", isSuccess);
			inforMap.put("info", info);
			return inforMap;
		}
		String cardId = cardObj.toString();
		String orgNo = orgObj.toString();
		Map<String, Object> addMap = new HashMap<String, Object>();
		Map<String, Object> employMap = new HashMap<String, Object>();
		employMap.put("CardID", cardId);
		String employKeys = "OrgNo,Source,SourceType,Nature,EmployType,EmployTime,TypeNo,Manager,RewardId";
		
		Set<String> keySet = inputMap.keySet();
		for(String key : keySet){//分离基本信息和入职信息
			if (employKeys.contains(key)) {
				employMap.put(key, inputMap.get(key));
			}else if(key.equals("OrgName")||key.equals("EmployId")){//此字段去除
				
			}else {
				addMap.put(key, inputMap.get(key));
			}
		}
		
		if ("edit".equals(action)) {//修改
			String employId = inputMap.get("EmployId").toString();
			Boolean update = commonDataService.update(operator, TABLE_NAME, "CardID", cardId, addMap);
			Boolean updateEmploy = commonDataService.update(operator, TABLE_NAME_EMPLOY, "Id", employId, employMap);
			isSuccess = update && updateEmploy;
			if (isSuccess) {
				info = "员工信息修改成功.";
			}else {
				info = "员工信息修改失败！";
			}
			inforMap.put("isSuccess", isSuccess);
			inforMap.put("info", info);
			return inforMap;
		}
		
		//查询是否可添加,如果不可添加，但依然可以入职
		Boolean checkUser = this.checkUser(cardId);
		if (checkUser) {
			//添加员工
			addMap.put("Password", MyMD5.circularlyEncrypt("123456", 3));
			isSuccess = commonDataService.add(operator, TABLE_NAME, addMap);
			//添加失败则返回
			if (!isSuccess) {
				info = "员工添加失败！";
				inforMap.put("isSuccess", isSuccess);
				inforMap.put("info", info);
				return inforMap;
			}
		}else {
			//如果修改失败，不需要返回
			isSuccess = commonDataService.update(operator, TABLE_NAME, "CardID", cardId, addMap);
		}
		//查询是否可入职
		Boolean checkEmploy = this.checkEmploy(cardId);
		if (!checkEmploy) {
			isSuccess = false;
			info = "该员工不能重复入职！";
			inforMap.put("isSuccess", isSuccess);
			inforMap.put("info", info);
			return inforMap;
		}
		Integer nature = 1;//默认入职的都是长期工
		if (natureObj != null) {
			nature = Integer.parseInt(natureObj.toString());
		}
		Integer sourceType = null;
		if (stObj != null) {
			sourceType = Integer.parseInt(stObj.toString());
		}
		
		//查询所在公司在员工入职时间的返费配置并记录到入职表
/*		if (timeObj != null && sourceType != null) {
			List<Map<String, Object>> list = rewardConfig.getList(orgNo, DateUtil.toDate(timeObj.toString()),nature,sourceType);
			if (list.size() > 0) {
				Map<String, Object> map = list.get(0);//公司的最新返费配置
				employMap.put("RewardId", map.get("Id"));
				employMap.put("LimitCount", map.get("LimitCount").toString());//剩余返费次数
				employMap.put("ExtraLimit", map.get("ExtraLimit"));//剩余满返次数
			}
		}*/
		//HelloWorld_luo 根据选择配置返费配置
		if (policyObj!=null) {
			String rewardId = policyObj.toString();
			if(StringUtils.isEmpty(rewardId)){
				if (timeObj != null && sourceType != null) {
					List<Map<String, Object>> list = rewardConfig.getList(orgNo, DateUtil.toDate(timeObj.toString()),nature,sourceType);
					if (list.size() > 0) {
						Map<String, Object> map = list.get(0);//公司的最新返费配置
						employMap.put("RewardId", map.get("Id"));
						employMap.put("LimitCount", map.get("LimitCount").toString());//剩余返费次数
						employMap.put("ExtraLimit", map.get("ExtraLimit"));//剩余满返次数
					}
				}
			}else{
				Map<String, Object> map = rewardConfig.getForm(rewardId);
				employMap.put("RewardId", map.get("Id"));
				employMap.put("LimitCount", map.get("LimitCount").toString());//剩余返费次数
				employMap.put("ExtraLimit", map.get("ExtraLimit"));//剩余满返次数
			}
		}
		//员工入职
		isSuccess = commonDataService.add(operator, TABLE_NAME_EMPLOY, employMap);
		if (isSuccess) {//入职成功后，返回该条数据的ID
			inforMap.put("EmployId", this.getEmployId(cardId, orgNo));
		}
		inforMap.put("isSuccess", isSuccess);
		inforMap.put("info", isSuccess ? "员工信息录入成功。" : "员工信息录入失败！");
		return inforMap;
	}
	/**
	 * 员工批量添加并入职
	 * @param operator
	 * @param inputList
	 * @return
	 * @author ServerZhang
	 * @date 2017年3月20日
	 */
	@Override
	public Map<String, Object> addAndEmployBatch(String operator, 
			List<Map<String, Object>> inputList){
		
		//来源分类对应来源编号
		Map<Object, Object> sourceTypeMap = new HashMap<Object, Object>();
		
		List<Map<String, Object>> sourceType = commonDataService.getType("reward.source_type");
		sourceType.forEach(row -> {
			sourceTypeMap.put(row.get("SourceType"), row.get("Id"));
		});
		
		List<Map<String, Object>> tempAdd = new ArrayList<Map<String, Object>>();
		List<Map<String, Object>> tempEmploy = new ArrayList<Map<String, Object>>();
		
		String employKeys = "OrgNo,Source,SourceType,Nature,EmployType,EmployTime,TypeNo,Manager";
		Iterator<Map<String, Object>> iterator = inputList.iterator();
		while(iterator.hasNext()){
			Map<String, Object> next = iterator.next();
			Object cardObj = next.get("CardID");
			Object orgObj = next.get("OrgNo");
			Object natureObj = next.get("Nature");
			Object sourceObj = next.get("SourceType");
			Object sexObj = next.get("Sex");
			Object birthObj = next.get("BirthDay");
			Object timeObj = next.get("EmployTime");
			Object phoneObj = next.get("PhoneNo");
			
			if (cardObj == null || orgObj == null) {
				continue;
			}
			
			String cardId = cardObj.toString();
			String orgNo = orgObj.toString();
			//通过身份证号自动填充户籍地
			String province = cardId.substring(0, 2);
			String city = cardId.substring(0, 4);
			String area = cardId.substring(0, 6);
			
			next.put("Province", province+"0000");
			next.put("City", city+"00");
			next.put("Area", area);
			
			if (phoneObj != null) {
				String phoneNo = phoneObj.toString();
				if (phoneNo.contains(".")) {
					String replace = phoneNo.replace(".", "");
					String[] split = replace.split("E");
					if (split.length > 0) {
						phoneNo = split[0];
						next.put("PhoneNo", split[0]);
					}
				}
			}
			
			if (birthObj != null) {
				String birth = birthObj.toString();
				Calendar instance = Calendar.getInstance();
				String[] split = birth.split("-");
				if (split.length == 3) {
					if (this.months.size()<1) {
						this.loadMonth();
					}
					instance.set(Integer.parseInt(split[2]), months.get(split[1])-1, Integer.parseInt(split[0]));
					Date time = instance.getTime();
					next.put("BirthDay", DateUtil.getDateStr(time, DateType.YYYY_MM_DD));
				}else {
					next.put("BirthDay", "");
				}
			}
			if (sexObj != null){
				if("男".equals(sexObj.toString())){
					next.put("Sex", 1);
				}else if ("女".equals(sexObj.toString())) {
					next.put("Sex", 0);
				}else {
					next.put("Sex", -1);
				}
			}
			Integer sourceTypeId = null;
			if (sourceObj != null){
				Object object = sourceTypeMap.get(sourceObj);
				if (object != null) {
					sourceTypeId = Integer.parseInt(object.toString());
					next.put("SourceType", sourceTypeId);
				}
			}
			
			Map<String, Object> addMap = new HashMap<String, Object>();
			Map<String, Object> employMap = new HashMap<String, Object>();
			employMap.put("CardID", cardId);
			
			Integer nature = 1;//默认入职的都是长期工
			if (natureObj != null && natureObj.toString().contains("短")) {
				nature = 0;
			}
			next.put("Nature", nature);
			//查询所在公司当前时间的返费配置并记录到入职表
			if (timeObj != null) {
				Date time = null;
				String timeStr = timeObj.toString();
				Calendar instance = Calendar.getInstance();
				String[] split = timeStr.split("-");
				if (split.length == 3) {
					if (this.months.size()<1) {
						this.loadMonth();
					}
					instance.set(Integer.parseInt(split[2]), months.get(split[1])-1, Integer.parseInt(split[0]));
					time = instance.getTime();
					next.put("EmployTime", DateUtil.getDateStr(time, DateType.YYYY_MM_DD));
				}else {
					next.put("EmployTime", "");
				}
				if (sourceTypeId != null) {
					List<Map<String, Object>> list = rewardConfig.getList(orgNo, time,nature,sourceTypeId);
					if (list.size() > 0) {
						Map<String, Object> map = list.get(0);//公司的最新返费配置
						employMap.put("RewardId", map.get("Id"));
						employMap.put("LimitCount", map.get("LimitCount").toString());//剩余返费次数
						employMap.put("ExtraLimit", map.get("ExtraLimit"));//剩余满返次数
					}
				}
			}
			
			Set<String> keySet = next.keySet();
			for(String key : keySet){//分离基本信息和入职信息
				if (employKeys.contains(key)) {
					employMap.put(key, next.get(key));
				}else if(key.equals("OrgName")||key.equals("EmployId")){//此字段去除
					
				}else {
					addMap.put(key, next.get(key));
				}
			}
			addMap.put("Password", MyMD5.circularlyEncrypt("123456", 3));
			tempAdd.add(addMap);
			tempEmploy.add(employMap);
		}
		Map<String, List<Map<String, Object>>> addOrUpdateMap = this.checkUser(tempAdd);
		
		List<Map<String, Object>> canAddList = addOrUpdateMap.get("add");//可以添加的员工
		List<Map<String, Object>> canUpdateList = addOrUpdateMap.get("update");//可以修改信息的员工
		//List<Map<String, Object>> canEmployList = this.checkEmploy(tempEmploy);//可以入职的员工
		
		commonDataService.addBatch(operator, TABLE_NAME, canAddList);
		commonDataService.updateBatch(operator, TABLE_NAME, "CardID", canUpdateList);
		this.employBatch(operator, tempEmploy);
		
		//返回信息
		Map<String, Object> inforMap = new HashMap<String, Object>();
		Boolean isSuccess = true;
		String info = "";
		inforMap.put("isSuccess", isSuccess);
		inforMap.put("info", info);
		return inforMap;
	}
	public Integer getEmployId (String cardId, String orgNo){
		String sql = "select Id from %s where CardID='%s' and OrgNo='%s' and state=1";
		return jdbcTemplate.queryForObject(String.format(sql, TABLE_NAME_EMPLOY,cardId,orgNo), Integer.class);
	}
	/**
	 * 离职
	 * @param employIds
	 * @return
	 * @author ServerZhang
	 * @date 2017年3月8日
	 */
	@Override
	public Boolean delete(String operator,String employIds){
		String sql = "update %s set State=0,LeaveTime='%s',UpdateTime='%s',Mender='%s' where Id in ('%s')";
		String dateStr = DateUtil.getDateStr(new Date(),DateType.YYYY_MM_DD_HH_mm_ss_SSS);
		return jdbcTemplate.update(String.format(sql, TABLE_NAME_EMPLOY,dateStr,dateStr,operator,employIds.replaceAll(",", "','"))) > 0;
	}
	/**
	 * 重新入职
	 * @param employIds
	 * @return
	 * @author ServerZhang
	 * @date 2017年3月8日
	 */
	@Override
	public Boolean employ(String operator, String employIds){
		String sql = "update %s set State=1,LeaveTime=null,UpdateTime='%s',Mender='%s' where Id in ('%s')";
		String dateStr = DateUtil.getDateStr(new Date(),DateType.YYYY_MM_DD_HH_mm_ss_SSS);
		return jdbcTemplate.update(String.format(sql, TABLE_NAME_EMPLOY,dateStr,operator,employIds.replaceAll(",", "','"))) > 0;
		/*String sql = "select * from %s where Id in ('%s')";
		List<Map<String, Object>> list = jdbcTemplate.queryForList(String.format(sql, TABLE_NAME_EMPLOY,employIds));
		return employBatch(operator, list);*/
	}
	 /**
	 * 获取员工属性配置，生成前端字符串
	 * @return
	 * @author ServerZhang
	 * @date 2017年3月10日
	 */
	@Override
	public String getConfigStr(){
		StringBuffer sBuffer = new StringBuffer();
		List<Map<String, Object>> list = userConfigService.getList();
		Iterator<Map<String, Object>> iterator = list.iterator();
		while(iterator.hasNext()){
			Map<String, Object> next = iterator.next();
			Object fieldObj = next.get("FieldName");
			Object contentObj = next.get("Content");
			Object isLongObj = next.get("IsLong");
			if (fieldObj == null || contentObj == null || isLongObj == null) {
				continue;
			}
			Boolean isLong = (Boolean)isLongObj;
			
			sBuffer.append("<tr><td class='fieldTitle'>").append(contentObj.toString())
				.append("</td><td class='fieldContent' colspan='3'>");
			if (isLong) {
				sBuffer.append("<textarea name='")
				.append(fieldObj.toString())
				.append("' class='mini-textarea' style='width: 100%;height: 100px;' emptyText='无' ></textarea></td></tr>");
			}else {
				sBuffer.append("<input name='")
				.append(fieldObj.toString())
				.append("' class='mini-textbox' style='width: 100%;' /></td></tr>");
			}
		}
		return sBuffer.toString();
	}
	/**
	 * 获取员工属性配置，APP前端用
	 * @return
	 * @author ServerZhang
	 * @date 2017年3月18日
	 */
	@Override
	public List<Map<String, Object>> getConfigStr4App(){
		List<Map<String, Object>> list = userConfigService.getList();
		return list;
	}
	 /**
	 * 批量考勤
	 * @param operator
	 * @param employIds
	 * @param days
	 * @return
	 * @author ServerZhang
	 * @date 2017年3月22日
	 */
	@Override
	public Boolean checkIn(String operator, String employIds, Float days){
		String sql = "update %s set Checked=1,CheckInTime='%s',CheckInDay=CheckInDay+%s,CurrentDay=CurrentDay+%s where Id in ('%s')";
		String dateStr = DateUtil.getDateStr(new Date(),DateType.YYYY_MM_DD_HH_mm_ss_SSS);
		String[] split = employIds.split(",");
		List<Map<String, Object>> list = new ArrayList<>();
		for(String s : split){
			Map<String, Object> map =  new HashMap<String, Object>();
			map.put("EmployId", s);
			map.put("CheckInDay", days);
			list.add(map);
		}
		commonDataService.addBatch(operator, "reward.employ_check_in", list);
		
		return jdbcTemplate.update(String.format(sql, TABLE_NAME_EMPLOY,dateStr,days,days,employIds.replaceAll(",", "','"))) > 0;
	}
	 /**
	 * 初始化次日考勤
	 * @author ServerZhang
	 * @date 2017年3月22日
	 */
	@Override
	public void iniCheckIn(){
		String sql = "update %s set Checked=0 where Nature=0";
		jdbcTemplate.update(String.format(sql, TABLE_NAME_EMPLOY));
	}
	@Override
	public Map<String, Object> getFormByCard(String cardId){
		Map<String, Object> formData = commonDataService.getFormData("reward.v_user_info", "CardID", cardId, null, null);
		return formData;
	}
	@Override
	public DataVector<Map<String, Object>> getCheckInList(RequestInput requestInput,
			Integer employId) {
		String whereStr = String
				.format("employId=%s", employId);
		String sortStr = "CreateTime desc";
		return commonDataService.getListData("reward.v_employ_check_in", whereStr, sortStr,
				requestInput);
	}
	
}
